<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Medal_Offer extends CI_Controller {

    public function Medal_Offer() {
        parent::__construct();
        $this->load->model('user_model');
        //$this -> load -> model('medal_recept_model');
        $this->load->library('administrator_authenticate');
    }

    public function index() {
        $this->administrator_authenticate->check_login();
        $data['content'] = 'medal_offer_view';
        $data['header'] = 'សៀវភៅមេដាយ';
        $data['jquery_ui'] = '';
        $data['jquery_dialog'] = '';
        $data['officialOrders'] = $this->_getOfficialOrder();
        $data['medaltypes'] = $this->_getMedalType();
        $this->load->view('admin_template', $data);
    }

    private function _getOfficialOrder() {
        return $this->db->query("SELECT ID, Order_Number FROM Official_Order ORDER BY id LIMIT 500")->result_array();
    }

    private function _getMedalType() {
        return $this->db->query("SELECT ID, name FROM Medal_Type")->result_array();
    }

    public function offer() {
        $this->administrator_authenticate->check_login();
        $this->load->model('consignee_model');

        if (trim(@$_POST['offerid']) != "") { // UPDATE
            $return = $this->consignee_model->update();
        } else {
            $return = $this->consignee_model->add();
        }
        echo json_encode($return);
    }

    public function offer_list() {
        $this->administrator_authenticate->check_login();
        $this->load->model('consignee_model');
        $output = $this->consignee_model->get_consignee();
        echo json_encode($output);
    }

    public function read_offer() {
        $this->administrator_authenticate->check_login();
        $result = $this->db->query("SELECT * FROM Consignee WHERE ID = " . check_post("id") . " ORDER BY id LIMIT 1")->result_array();
        echo json_encode($result[0]);
    }

    public function read_medal() {
        $this->administrator_authenticate->check_login();
        $result = $this->db->query("
            SELECT Medal.ID, Medal.Medal_Class_ID, Medal.Medal_Type_ID, Medal_Consignee.amount FROM Medal_Consignee
            RIGHT JOIN Medal ON Medal.ID = Medal_Consignee.medal_ID AND Medal_Consignee.consignee_ID =" . check_post("id"))->result_array();
        echo json_encode($result);
    }

    public function deleteOffer() {
        $this->administrator_authenticate->check_login();
        $this->db->delete('Medal_Consignee', array('consignee_ID' => check_post("ID")));
        $this->db->delete('Consignee', array('ID' => check_post("ID")));
        $return = array();
        if ($this->db->affected_rows() > 0) {
            $return['result'] = true;
            $return['message'] = 'លុបអ្នកយកមេដាយបានជោគជ័យ.';
        } else {
            $return['result'] = false;
            $return['message'] = 'មិនអាចលុបអ្នកយកមេដាយបាន សូមព្យាយាមម្តងទៀត';
        }
        echo json_encode($return);
    }

    public function stock_summary() {
        $id = check_post("ID");
        $result['stock'] = $this->db->query("
           SELECT Medal_Stock.medal_ID, Medal.Medal_Type_ID, Medal.Medal_Class_ID, Medal_Type.name, Medal_Stock.amount from Medal_Stock
                RIGHT JOIN Medal ON Medal.ID = Medal_Stock.Medal_ID
                INNER JOIN Medal_Type ON Medal.Medal_Type_ID = Medal_Type.ID
                WHERE Medal_Type.ID = '$id'
           ")->result_array();
        $result['summary'] = $this->db->query("
        SELECT Medal.ID, Medal.Medal_Class_ID, Medal.Medal_Type_ID, offer_summary.amount FROM offer_summary
            RIGHT JOIN Medal ON Medal.ID = offer_summary.medal_id 
                INNER JOIN Medal_Type ON Medal_Type.ID = Medal.Medal_Type_ID
                WHERE Medal.Medal_Type_ID = '$id'
           ")->result_array();

        echo json_encode($result);
    }

}